﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SIS.BLL;
using SIS.ENT;

public partial class student_fee_payment : System.Web.UI.Page
{
    SIS.UI.Common _common = new SIS.UI.Common();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Page.Request.Params["feetypecode"] != null && Page.Request.Params["paymentref"] != null)
            {
                int _intPaymentRef = 0;
                if (int.TryParse(Request.Params["paymentref"], out _intPaymentRef))
                {
                    txtPaymentRefKey.Text = _intPaymentRef.ToString();
                    txtPaymentRefKeyValidation.Text = _intPaymentRef.ToString();
                    if (Page.Request.Params["feetypecode"].ToString() == "1001")
                    {
                        StudentRegisterationTable _register = new StudentRegisterationTable();
                        _register = StudentRegisterationTableManager.GetItem(_intPaymentRef);

                        if (_register != null)
                        {
                            hplStudent.NavigateUrl = "~/student/?id=" + _register.UserAccountKey.ToString();
                            ddlSchoolList.DataBind();

                            _common.LoadRelationshipTypeDDL(ddlGSRelation, string.Empty);

                            ddlSchoolList.DataBind();
                            ddlSchoolList.Text = _register.SchoolKey.ToString();
                            ddlSchoolList_SelectedIndexChanged(sender, e);

                            ddlSchoolMajor.Text = _register.SchoolMajorKey.ToString();
                            ddlSchoolMajor_SelectedIndexChanged(sender, e);
                            ddlSchoolMajor.Enabled = false;

                            ddlStandardLevel.Text = _register.StandardLevelKey.ToString();
                            ddlStandardLevel_SelectedIndexChanged(sender, e);
                            ddlStandardLevel.Enabled = false;

                            ddlLocation.Text = _register.LocationKey.ToString();
                            ddlLocation_SelectedIndexChanged(sender, e);
                            ddlLocation.Enabled = false;

                            ddlPeriod.Text = _register.EducationPeriodKey.ToString();

                            ddlFeeTypeCode.DataBind();
                            ddlFeeTypeCode.Text = "1001";
                            ddlFeeTypeCode_SelectedIndexChanged(sender, e);

                            LoadStudentInfo(_register.UserAccountKey);
                            Int32 _pSFKey = 0;
                            if (Page.Request.Params["sfkey"] != null && Int32.TryParse(Page.Request.Params["sfkey"].ToString(), out _pSFKey))
                            {
                                if (grvStudentFee.Rows.Count > 0)
                                {
                                    Int32 _sfkey = 0;
                                    //Finding in Student Fee Setup
                                    foreach (GridViewRow gvr in grvStudentFee.Rows)
                                    {
                                        _sfkey = Convert.ToInt32(grvStudentFee.DataKeys[gvr.RowIndex].Values[0].ToString());
                                        if (_sfkey == _pSFKey)
                                        {
                                            grvStudentFee.SelectedIndex = gvr.RowIndex;
                                            //txtStudentFeeKey.Text = _pSFKey.ToString();
                                            grvStudentFee_SelectedIndexChanged(sender, e);

                                            Int32 _pSFDKey = 0;
                                            if (Page.Request.Params["sfdkey"] != null && Int32.TryParse(Page.Request.Params["sfdkey"].ToString(), out _pSFDKey))
                                            {
                                                Int32 _sfdkey = 0;
                                                if (grvStudentFeeDetail.Rows.Count > 0)
                                                {
                                                    //Finding in Student Fee Details
                                                    foreach (GridViewRow gvr2 in grvStudentFeeDetail.Rows)
                                                    {
                                                        _sfdkey = Convert.ToInt32(grvStudentFeeDetail.DataKeys[gvr2.RowIndex].Values[0].ToString());
                                                        if (_sfdkey == _pSFDKey)
                                                        {
                                                            grvStudentFeeDetail.SelectedIndex = gvr2.RowIndex;
                                                            grvStudentFeeDetail_SelectedIndexChanged(sender, e);
                                                            //txtSFDKey.Text = _pSFDKey.ToString();
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (grvStudentFeeDetail.Rows.Count > 0)
                                                {
                                                    grvStudentFeeDetail.SelectedIndex = 0;
                                                    grvStudentFeeDetail_SelectedIndexChanged(sender, e);
                                                }
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (grvStudentFee.Rows.Count > 0)
                                {
                                    grvStudentFee.SelectedIndex = 0;
                                    grvStudentFee_SelectedIndexChanged(sender, e);
                                }

                                if (grvStudentFeeDetail.Rows.Count > 0)
                                {
                                    grvStudentFeeDetail.SelectedIndex = 0;
                                    grvStudentFeeDetail_SelectedIndexChanged(sender, e);
                                }
                            }
                        }
                        else
                        {

                        }
                    }
                }


            }
        }
    }

    private bool LoadStudentInfo(int AccountID)
    {
        bool _retValue = true;
        UserAccountTable _account = new UserAccountTable();
        _account = UserAccountTableManager.GetItem(AccountID);

        if (_account == null)
        {
            lblTopMessage.Text = "Student account ID (" + AccountID.ToString() + ") is not found!";
            hplStudent.Visible = false;
            _retValue = false;
        }
        else if (_account.UserAccountKey <= 0)
        {
            lblTopMessage.Text = "Student account ID is not found!";
            hplStudent.Visible = false;
            _retValue = false;
        }

        if (_account != null && _account.UserAccountKey > 0)
        {
            if (_account.UserAccountTypeCode != 300)
            {
                lblTopMessage.Text = "Only for student account ID will be loaded.";
                hplStudent.Visible = false;
                _retValue = false;
            }

            lblUserAccountKey.Text = _account.UserAccountKey.ToString();

            StudentProfileTable _sprofile = new StudentProfileTable();
            StudentAddressTable _saddress = new StudentAddressTable();
            StudentContactTable _scontact = new StudentContactTable();
            StudentGuardianRelationshipTable _gsrelation = new StudentGuardianRelationshipTable();
            ddlSchoolList.Text = _account.SchoolKey.ToString();
            ddlSchoolList.Enabled = false;

            _sprofile = StudentProfileTableManager.GetItemByUserAccountKey(AccountID);
            if (_sprofile != null && _sprofile.StudentProfileKey > 0)
            {
                lblStudentProfileKey.Text = _sprofile.StudentProfileKey.ToString();

                //txtFirstName.Text = _sprofile.FirstName;
                //txtLastName.Text = _sprofile.LastName;
                //txtOtherName.Text = _sprofile.OtherName;
                string _fullName = string.Empty;
                if (_sprofile.FirstName.Length > 0)
                {
                    _fullName = _sprofile.FirstName;
                }

                if (_sprofile.LastName.Length > 0)
                {
                    _fullName += " " + _sprofile.LastName;
                }

                if (_sprofile.OtherName.Length > 0)
                {
                    _fullName += " &lt;or&gt; " + _sprofile.OtherName;
                }
                //lblFullName.Text = _fullName;
                //_fullName = " - " + _sprofile.Gender + " ";

                DateTime _dtDOB = _sprofile.DateOfBirth;
                int _age = DateTime.Now.Year - _dtDOB.Year;
                _fullName += " ( " + _sprofile.Gender + ", " + _age.ToString() + " yrs )";
                lblFullName.Text = _fullName;
                //ddlDOBDay.Text = _dtDOB.Day.ToString("00");
                //ddlDOBMonth.Text = _dtDOB.Month.ToString("00");
                //ddlDOBYear.Text = _dtDOB.Year.ToString("0000");

                //txtNRICNumber.Text = _sprofile.NRICNumber;
                //ddlDrivingLicenceType.Text = _sprofile.DrivingLicenceType.ToString();
                //txtDrivingLicenceNumber.Text = _sprofile.DrivingLicenseNumber;

                //ddlTravelDocType.Text = _sprofile.TravelDocumentType.ToString();
                //txtTravelDocNumber.Text = _sprofile.TravelDocumentNumber;

                //ddlBloodGroup.Text = _sprofile.BloodGroupCode.ToString();
                //ddlMaritalStatus.Text = _sprofile.MartialStatusCode.ToString();
                //ddlRaceCode.Text = _sprofile.RaceCode.ToString();
                //ddlReligion.Text = _sprofile.ReligionCode.ToString();
                //ddlNationality.Text = _sprofile.NationalityCode.ToString();
                //txtWebsiteUrl.Text = _sprofile.WebSiteUrl;
                //txtDisablityRemark.Text = _sprofile.DisabilityRemark;
                //txtRemark.Text = _sprofile.Remark;
            }
            else
            {
                _retValue = false;
            }

            _saddress = StudentAddressTableManager.GetItemByUserAccountKey(AccountID);
            if (_saddress != null && _saddress.StudentAddressKey > 0)
            {
                //lblStudentAddressKey.Text = _saddress.StudentAddressKey.ToString();
                //txtUnitNo.Text = _saddress.UnitNo;
                //txtStreetName.Text = _saddress.StreetName;
                //ddlCity.Text = _saddress.CityCode.ToString();
                //ddlDistrict.Text = _saddress.DistrictCode.ToString();
                //ddlCountry.Text = _saddress.CountryCode.ToString();
                //txtPostCode.Text = _saddress.PostalCode;
            }
            else
            {
                _retValue = false;
            }

            _scontact = StudentContactTableManager.GetItemByUserAccountKey(AccountID);
            if (_scontact != null && _scontact.StudentContactKey > 0)
            {
                string _contactInfo = "<ul>";
                if (_scontact.MobileNumber != null && _scontact.MobileNumber.Length > 0)
                {
                    _contactInfo = "<li>Mobile: " + _scontact.MobileNumber + "<li>";
                }

                if (_scontact.HomePhoneNumber != null && _scontact.HomePhoneNumber.Length > 0)
                {
                    _contactInfo += "<li>Home: " + _scontact.HomePhoneNumber + "</li>";
                }

                if (_scontact.OtherNumber != null && _scontact.OtherNumber.Length > 0)
                {
                    //if (_contactInfo.Length > 0)
                    //    _contactInfo += ", ";

                    _contactInfo += "<li>Other: " + _scontact.OtherNumber + "</li>";
                }

                if (_scontact.EmailAddress != null && _scontact.EmailAddress.Length > 0)
                {
                    //if (_contactInfo.Length > 0)
                    //    _contactInfo += ", ";

                    _contactInfo += "<li>Email: " + _scontact.EmailAddress + "</li>";
                }
                _contactInfo += "</ul>";

                lblContactInfo.Text = _contactInfo;
                //lblStudentContactKey.Text = _scontact.StudentContactKey.ToString();
                //txtMobileNumber.Text = _scontact.MobileNumber;
                //txtHomePhoneNumber.Text = _scontact.HomePhoneNumber;
                //txtEmailAddress.Text = _scontact.EmailAddress;
                //txtOtherContactNumber.Text = _scontact.OtherNumber;
            }
            else
            {
                _retValue = false;
            }

            _gsrelation = StudentGuardianRelationshipTableManager.GetItemByStudentAccountKey(AccountID, true);

            if (_gsrelation != null && _gsrelation.GuardianAccountKey > 0)
            {

                //lblStudentGuardianRelationTypeKey.Text = _gsrelation.GSAutoID.ToString();
                UserAccountTable _gaccount = new UserAccountTable();
                _gaccount = UserAccountTableManager.GetItem(_gsrelation.GuardianAccountKey);

                if (_gaccount != null && _gaccount.UserAccountKey > 0)
                {
                    //lblGuardianAccountKey.Text = _gaccount.UserAccountKey.ToString();
                    GuardianProfileTable _gprofile = new GuardianProfileTable();
                    GuardianAddressTable _gaddress = new GuardianAddressTable();
                    GuardianContactTable _gcontact = new GuardianContactTable();

                    _gprofile = GuardianProfileTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey);
                    if (_gprofile != null && _gprofile.GuardianProfileKey > 0)
                    {
                        string _fullName = string.Empty;
                        if (_gprofile.FirstName.Length > 0)
                        {
                            _fullName = _gprofile.FirstName;
                        }

                        if (_gprofile.LastName.Length > 0)
                        {
                            _fullName += " " + _gprofile.LastName;
                        }

                        if (_gprofile.OtherName != null && _gprofile.OtherName.Length > 0)
                        {
                            _fullName += " ( " + _gprofile.OtherName + " )";
                        }

                        ddlGSRelation.Text = _gsrelation.RelationshipTypeCode.ToString();
                        ddlGSRelation.Enabled = false;
                        lblGuardianFullName.Text = _fullName;
                        //lblGuardianProfileKey.Text = _gprofile.GuardianProfileKey.ToString();
                        //txtGuardianFirstName.Text = _gprofile.FirstName;
                        //txtGuardianLastName.Text = _gprofile.LastName;
                        DateTime _dtGDOB = _gprofile.DateOfBirth;
                        //ddlGuardianDOBDay.Text = _dtGDOB.Day.ToString("00");
                        //ddlGuardianDOBMonth.Text = _dtGDOB.Month.ToString("00");
                        //ddlGuardianDOBYear.Text = _dtGDOB.Year.ToString("0000");
                        //ddlGuardianRelationshipType.Text = _gsrelation.RelationshipTypeCode.ToString();
                    }

                    _gaddress = GuardianAddressTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey, 1);
                    if (_gaddress != null && _gaddress.GuardianAddressKey > 0)
                    {
                        //lblGuardianAddressKey.Text = _gaddress.GuardianAddressKey.ToString();
                        //txtGuardianAddressUnitNo.Text = _gaddress.UnitNo;
                        //txtGuardianAddressStreet.Text = _gaddress.StreetName;
                        //txtGuardianAddressPostal.Text = _gaddress.PostalCode;
                        //ddlGuardianAddressCountry.Text = _gaddress.CountryCode.ToString();
                    }

                    _gcontact = GuardianContactTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey);
                    if (_gcontact != null && _gcontact.GuardianContactKey > 0)
                    {
                        //lblGuardianContactKey.Text = _gcontact.GuardianContactKey.ToString();
                        //txtGuardianHomePhoneNumber.Text = _gcontact.HomePhoneNumber;
                        //txtGuardianMobileNumber.Text = _gcontact.MobileNumber;
                        string _contactInfo = "<ul>";
                        if (_gcontact.MobileNumber != null && _gcontact.MobileNumber.Length > 0)
                        {
                            _contactInfo = "<li>Mobile: " + _gcontact.MobileNumber + "<li>";
                        }

                        if (_gcontact.HomePhoneNumber != null && _gcontact.HomePhoneNumber.Length > 0)
                        {
                            _contactInfo += "<li>Home: " + _gcontact.HomePhoneNumber + "</li>";
                        }
                        _contactInfo += "</ul>";
                        lblGuardianContactInfo.Text = _contactInfo;
                    }
                }
                else
                {
                    _retValue = false;
                }
            }
            else
            {
                _retValue = false;
            }
        }
        return _retValue;
    }

    protected void btnSFPTSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("StudentFeePaymentTrans");
        if (Page.IsValid)
        {
            bool _IsValid = true;
            StudentFeePaymentTransactionTable _payment = new StudentFeePaymentTransactionTable();
            _payment.SFDKey = Convert.ToInt32(txtSFDKey.Text);
            _payment.SFPTKey = Convert.ToInt32(lblSFPTKey.Text);
            _payment.PaymentRefKey = Convert.ToInt32(txtPaymentRefKey.Text);

            _payment.PaymentBy = Convert.ToInt16(ddlPaymentBy.Text);
            _payment.PaymentModeCode = Convert.ToInt16(ddlPaymentMode.Text);
            _payment.PaymentModeDetail = txtPaymentModeDetail.Text;

            decimal _amount = 0.0M;
            if (decimal.TryParse(txtFDAmount.Text, out _amount))
            {
                if (_amount <= Convert.ToDecimal(txtFDAmountLimit.Text))
                {
                    _payment.Amount = _amount;
                }
                else
                {
                    lblFDAmountLimitMessage.Text = "OVER";
                    _IsValid = false;
                }
            }
            else
            {
                lblFDAmountLimitMessage.Text = "INVALID";
                _IsValid = false;
            }

            decimal _addAmount = 0.0M;
            if (decimal.TryParse(txtFDAddAmount.Text, out _addAmount))
            {
                if (_addAmount <= Convert.ToDecimal(txtFDAddAmountLimit.Text))
                {
                    _payment.AdditinalAmount = _addAmount;
                }
                else
                {
                    lblFDAddAmountLimitMessage.Text = "OVER";
                    _IsValid = false;
                }
            }
            else
            {
                lblFDAddAmountLimitMessage.Text = "INVALID";
                _IsValid = false;
            }

            decimal _otherAmount = 0.0M;
            if (decimal.TryParse(txtFDOtherAmount.Text, out _otherAmount))
            {
                if (_otherAmount <= Convert.ToDecimal(txtFDOtherAmountLimit.Text))
                {
                    _payment.OtherAmount = _otherAmount;
                }
                else
                {
                    lblFDOtherAmountLimitMessage.Text = "OVER";
                    _IsValid = false;
                }
            }
            else
            {
                lblFDOtherAmountLimitMessage.Text = "INVALID";
                _IsValid = false;
            }

            _payment.PaymentReceivedKey = 1;
            _payment.PaymentAccountKey = 1;

            _payment.Status = true;

            if (_IsValid)
            {
                if (StudentFeePaymentTransactionTableManager.Save(_payment) > 0)
                {
                    ClearFDFields();
                    grvStudentFeeDetail_SelectedIndexChanged(sender, e);
                    grvSFPT.DataBind();
                    lblSFPTMessage.Text = "Saved fee payment details successfully at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");
                }
                else
                {
                    lblSFPTMessage.Text = "Transaction failed at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");
                }
            }
            else
            {
                lblSFPTMessage.Text = "Please check your payment amounts.";
            }
        }
    }

    private void ClearFields()
    {
        txtStudentFeeKey.Text = "-1";
        txtStudentFeeKeyValidation.Text = "-1";

        ClearFDFields();
    }

    private void ClearFDFields()
    {
        txtFDDescription.Text = string.Empty;
        txtFDAmount.Text = string.Empty;
        txtFDAddAmount.Text = string.Empty;
        txtFDOtherAmount.Text = string.Empty;

        //txtSFDKey.Text = "-1";
        //txtSFDKeyValidation.Text = "-1";
        lblSFPTKey.Text = "-1";

        lblSFPTMessage.Text = string.Empty;
        txtPaymentModeDetail.Text = string.Empty;
        lblFDAmountLimitMessage.Text = string.Empty;
        lblFDAddAmountLimitMessage.Text = string.Empty;
        lblFDOtherAmountLimitMessage.Text = string.Empty;

        ddlPaymentBy.SelectedIndex = 0;
        ddlPaymentMode.SelectedIndex = 0;
    }

    protected void btnSFPTAddNew_Click(object sender, EventArgs e)
    {
        ClearFDFields();
    }

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSchoolMajor.DataBind();
    }

    protected void ddlSchoolMajor_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlStandardLevel.DataBind();
        //ddlPeriod.DataBind();
    }

    protected void ddlPeriod_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    protected void ddlStandardLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlLocation.DataBind();
    }

    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlPeriod.DataBind();
    }

    protected void ddlFeeTypeCode_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        grvStudentFee.DataBind();
        if (grvStudentFee.Rows.Count > 0)
        {
            grvStudentFee.SelectedIndex = 0;
            grvStudentFee_SelectedIndexChanged(sender, e);
        }
    }

    protected void grvStudentFee_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvStudentFee.SelectedIndex >= 0)
        {
            ClearFields();
            DataKey _selectedDataKey = grvStudentFee.SelectedDataKey;
            int _studentFeeKey = Convert.ToInt32(_selectedDataKey[0]);
            if (_studentFeeKey > 0)
            {
                StudentFeeTable _fee = new StudentFeeTable();
                _fee = StudentFeeTableManager.GetItem(_studentFeeKey);

                if (_fee != null)
                {
                    txtStudentFeeKey.Text = _fee.StudentFeeKey.ToString();
                    txtStudentFeeKeyValidation.Text = _fee.StudentFeeKey.ToString();
                    grvStudentFeeDetail.DataBind();
                    if (grvStudentFeeDetail.Rows.Count > 0)
                    {
                        grvStudentFeeDetail.SelectedIndex = 0;
                        grvStudentFeeDetail_SelectedIndexChanged(sender, e);
                    }
                    grvSFPT.DataBind();

                    tbcEntry.ActiveTab = tbpSFPT;
                }
            }
        }
    }

    protected void grvStudentFeeDetail_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvStudentFeeDetail.SelectedIndex >= 0)
        {
            ClearFDFields();
            DataKey _selectedDataKey = grvStudentFeeDetail.SelectedDataKey;
            long _sFDKey = Convert.ToInt64(_selectedDataKey[0]);
            int _studentFeeKey = Convert.ToInt32(txtStudentFeeKey.Text);
            if (_sFDKey > 0 && _studentFeeKey > 0)
            {
                StudentFeeDetail _detail = new StudentFeeDetail();
                _detail = StudentFeeDetailManager.GetItem(_sFDKey, _studentFeeKey);

                if (_detail != null)
                {
                    tbcEntry.Enabled = true;
                    ShowFDAmount(_detail, 0);
                    tbcEntry.ActiveTab = tbpSFPT;
                }
            }
        }
    }

    private void ShowFDAmount(StudentFeeDetail _detail, long _SFPTKey)
    {
        decimal _amount = 0.0M;
        decimal _addAmount = 0.0M;
        decimal _otherAmount = 0.0M;

        StudentFeePaymentTransactionTableList _paymentList = new StudentFeePaymentTransactionTableList();
        _paymentList = StudentFeePaymentTransactionTableManager.GetList(Convert.ToInt32(txtStudentFeeKey.Text), Convert.ToInt32(txtPaymentRefKey.Text));

        if (_paymentList != null && _paymentList.Count > 0)
        {
            foreach (StudentFeePaymentTransactionTable _payment in _paymentList)
            {
                if (_payment.SFDKey == _detail.SFDKey && _payment.SFPTKey != _SFPTKey)
                {
                    if (_payment.Amount > 0)
                    {
                        _amount += _payment.Amount;
                    }

                    if (_payment.AdditinalAmount > 0)
                    {
                        _addAmount += _payment.AdditinalAmount;
                    }

                    if (_payment.OtherAmount > 0)
                    {
                        _otherAmount += _payment.OtherAmount;
                    }
                }
            }
        }

        if (_amount == _detail.Amount
            && _addAmount == _detail.AdditionalAmount
            && _otherAmount == _detail.OtherAmount)
        {
            lblSFPTOutMessage.Text = "No outstanding balance for selected payment.";
            lblSFPTOutMessage.ForeColor = System.Drawing.Color.Green;
            btnSFPTSubmit.Enabled = false;
        }
        else
        {
            if (_SFPTKey == 0)
            {
                lblSFPTOutMessage.Text = "Found outstanding balance.";
                lblSFPTOutMessage.ForeColor = System.Drawing.Color.Red;
            }
            btnSFPTSubmit.Enabled = true;
        }

        decimal _outAmount = 0.0M;
        decimal _outAddAmount = 0.0M;
        decimal _outOtherAmount = 0.0M;

        _outAmount = _detail.Amount - _amount;
        _outAddAmount = _detail.AdditionalAmount - _addAmount;
        _outOtherAmount = _detail.OtherAmount - _otherAmount;

        txtFDAmountLimit.Text = _outAmount.ToString();
        txtFDAddAmountLimit.Text = _outAddAmount.ToString();
        txtFDOtherAmountLimit.Text = _outOtherAmount.ToString();

        if (_outAmount >= 1000)
        {
            txtFDAmount.Text = _outAmount.ToString("#,000.00");
            txtFDAmountLimit.Text = _outAmount.ToString("#,000.00");
        }
        else
        {
            txtFDAmount.Text = _outAmount.ToString("#.00");
            txtFDAmountLimit.Text = _outAmount.ToString("#.00");
        }

        if (_outAddAmount >= 1000)
        {
            txtFDAddAmount.Text = _outAddAmount.ToString("#,000.00");
            txtFDAddAmountLimit.Text = _outAddAmount.ToString("#,000.00");
        }
        else
        {
            txtFDAddAmount.Text = _outAddAmount.ToString("#.00");
            txtFDAddAmountLimit.Text = _outAddAmount.ToString("#.00");
        }

        if (_outOtherAmount >= 1000)
        {
            txtFDOtherAmount.Text = _outOtherAmount.ToString("#,000.00");
            txtFDOtherAmountLimit.Text = _outOtherAmount.ToString("#,000.00");
        }
        else
        {
            txtFDOtherAmount.Text = _outOtherAmount.ToString("#.00");
            txtFDOtherAmountLimit.Text = _outOtherAmount.ToString("#.00");
        }

        txtFDDescription.Text = _detail.Description;

        txtFDDescription.Enabled = false;
        txtFDAmountLimit.Enabled = false;
        txtFDAddAmountLimit.Enabled = false;
        txtFDOtherAmountLimit.Enabled = false;

        txtStudentFeeKey.Text = _detail.StudentFeeKey.ToString();
        txtStudentFeeKeyValidation.Text = _detail.StudentFeeKey.ToString();

        txtSFDKey.Text = _detail.SFDKey.ToString();
        txtSFDKeyValidation.Text = _detail.SFDKey.ToString();
    }

    protected void grvSFPT_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvSFPT.SelectedIndex >= 0)
        {
            //ClearFDFields();
            DataKey _selectedDataKey = grvSFPT.SelectedDataKey;
            long _sFPTKey = Convert.ToInt64(_selectedDataKey[0]);
            if (_sFPTKey > 0)
            {
                StudentFeePaymentTransactionTable _payment = new StudentFeePaymentTransactionTable();
                _payment = StudentFeePaymentTransactionTableManager.GetItem(_sFPTKey);

                if (_payment != null)
                {
                    tbcEntry.Enabled = true;
                    ddlPaymentBy.Text = _payment.PaymentBy.ToString();
                    ddlPaymentMode.Text = _payment.PaymentModeCode.ToString();
                    txtPaymentModeDetail.Text = _payment.PaymentModeDetail;

                    lblSFPTKey.Text = _payment.SFPTKey.ToString();
                    txtSFDKey.Text = _payment.SFDKey.ToString();
                    StudentFeeDetail _detail = new StudentFeeDetail();
                    _detail = StudentFeeDetailManager.GetItem(Convert.ToInt32(txtSFDKey.Text), Convert.ToInt32(txtStudentFeeKey.Text));

                    if (_detail != null)
                    {
                        tbcEntry.Enabled = true;
                        btnSFPTSubmit.Enabled = true;
                        lblSFPTMessage.Text = string.Empty;

                        ShowFDAmount(_detail, _sFPTKey);
                        txtSFDKey.Text = _detail.SFDKey.ToString();
                        tbcEntry.ActiveTab = tbpSFPT;
                    }

                    tbcEntry.ActiveTab = tbpSFPT;
                }
            }
        }
    }
}
